<template>
  <view>
    <product-box
      :productList="productList"
      :isFapin="true"
      v-if="!rule_show"
      @choose_bargin="choose_bargin"
    ></product-box>
    <view class="add_rule_container" v-show="rule_show">
      <uinput
        v-model="bargin_ruleForm.cut_item_title"
        placeholder="请输入自定义名称"
        :border="false"
        @input="change_cut_title"
        type="textarea"
        label="商品名称"
      />
      <view
        class="start_pick_container"
        @tap="choose_time"
        style="margin-bottom: 10rpx"
        v-if="!twitter"
      >
        <uinput
          v-model="value"
          :border="false"
          label="砍价倒计时时间"
          disabled
        />
        <view v-if="!countdown" class="placeholder">请选择砍价倒计时时间</view>
        <view v-if="bargin_ruleForm.hour" class="placeholder">{{
          countdown
        }}</view>
      </view>
      <uinput
        v-model="bargin_ruleForm.need_times"
        placeholder="请输入次数"
        :border="false"
        @input="change_need_times"
        label="需要砍多少次"
        v-if="twitter"
      />
      <uinput
        v-model="bargin_ruleForm.one_people_times"
        placeholder="请输入每人次数"
        :border="false"
        @input="change_per_times"
        label="每人次数(默认次数)"
        v-if="twitter"
      />
      <uinput
        v-model="bargin_ruleForm.price"
        placeholder="请输入商品价格"
        :border="false"
        disabled
        label="商品价格"
      />
      <uinput
        v-model="bargin_ruleForm.cut_price"
        placeholder="请输入保底价"
        :border="false"
        @input="change_cut_price"
        label="保底价"
        v-if="twitter"
      />
      <uinput
        v-model="bargin_ruleForm.success_times"
        placeholder="请输入已成功次数"
        :border="false"
        @input="change_suc_times"
        label="成功次数"
        v-if="twitter"
      />
      <uinput
        v-model="bargin_ruleForm.open_times"
        placeholder="请输入活动被开启次数"
        :border="false"
        @input="change_open_times"
        label="已开启次数"
        v-if="twitter"
      />
      <uinput
        v-model="bargin_ruleForm.limit_radius_pub"
        placeholder="范围数内可参与此砍价"
        :border="false"
        type="number"
        @input="change_limit_radius"
        label="半径/米"
        v-if="!twitter"
      />
      <van-cell v-if="twitter">
        <view slot="title">{{
          bargin_ruleForm.show_need_times == 1
            ? "显示砍价所需刀数"
            : "不显示所需刀数"
        }}</view>
        <view slot="">
          <van-switch
            :checked="bargin_ruleForm.show_need_times == 1 ? true : false"
            @change="change_show_need_times"
          />
        </view>
      </van-cell>
      <van-cell v-if="twitter">
        <view slot="title">{{
          bargin_ruleForm.show_bj_price == 1 ? "显示保底价" : "不显示保底价"
        }}</view>
        <view slot="">
          <van-switch
            :checked="bargin_ruleForm.show_bj_price == 1 ? true : false"
            @change="change_show_bj_price"
          />
        </view>
      </van-cell>
      <view style="padding-left: 30upx">图片(图片大小不超过1m)</view>
      <view class="upload" style="padding-left: 20upx">
        <uImg
          ref="upimg"
          :canUploadFile="true"
          :limit="limit"
          :uploadFileUrl="uploadFileUrl"
          :uImgList.sync="uImgList"
          @uploadSuccess="uploadSuccess"
          :fileKeyName="name"
        />
        <!-- <van-button
          type="primary"
          size="mini"
          style="margin-left: 20upx"
          @tap="upload"
          >确定上传</van-button
        > -->
      </view>
      <div class="btn_box">
        <van-button @tap="submit" size="large" color="#f95a4b">提交</van-button>
        <van-button @tap="cancle_submit" size="large">返回</van-button>
      </div>
    </view>

    <w-picker
      :visible="show_time_picker"
      mode="time"
      :value="bargin_ruleForm.hour"
      :second="false"
      @confirm="confirm_time1"
      @cancel="cancle_time1"
      ref="date1"
    ></w-picker>
    <van-toast id="van-toast" />
  </view>
</template>

<script>
import uinput from "../../components/poiuy-uinput/uinput";
import wPicker from "@/components/w-picker/w-picker.vue";
import ProductBox from "../components/productBox";
import { query_xp_goods, item_save } from "../../utils/imapi";
import uImg from "../../components/zhtx-uploadImg/zhtx-uploadImg";
export default {
  props: {},
  components: {
    ProductBox,
    uImg,
    wPicker,
    uinput,
  },
  data() {
    return {
      productList: [],
      queryparam1: {
        page_index: 1,
        page_size: 10,
        goods_status: "",
        goods_id: "",
        for_src:'3'
      },
      rule_show: false,
      show: false,
      bargin_ruleForm: {
        item_id: "",
        p: "XP",
        status: 0,
        hour: "",
        minutes: "",
        need_times: "",
        one_people_times: 1,
        cut_item_title: "",
        pos_pic: "",
        price: 0,
        open_times: 0,
        success_times: 0,
        cut_price: "",
        cut_mode: "",
        show_need_times: 0,
        show_bj_price: 0,
        limit_radius_pub: "",
      },
      uImgList: [],
      value: "",
      uploadFileUrl:
        "https://api.comm.t.xingkonglian.net:8098/api/v1/oss/upload_starx",
      limit: 1,
      name: "file",
      show_time_picker: false,
      minHour: "00",
      maxHour: "23",
      countdown: "",
      twitter: false,
      can_submit: true,
    };
  },
  computed: {},
  watch: {},
  onLoad(options) {
    if (options.from) {
      if (options.from == "twitter") {
        this.twitter = true;
        this.bargin_ruleForm.hour = "2";
        this.bargin_ruleForm.minutes = "0";
        this.bargin_ruleForm.cut_mode = "2";
      } else if (options.from == "near") {
        this.twitter = false;
        this.bargin_ruleForm.cut_mode = "3";
        this.bargin_ruleForm.need_times = 20;
        this.bargin_ruleForm.show_need_times = 1;
        this.bargin_ruleForm.show_bj_price = 1;
        this.bargin_ruleForm.cut_price = 0;
      }
    }
    this.queryparam1.goods_status = options.goods_status;
    
  },
  onReady() {},
  onShow() {
    this.query_xp_goods();
  },
  methods: {
    uploadSuccess(result) {
      if (result.statusCode == 200) {
        //上传成功的回调处理
        console.log(result.data);
        const img = JSON.parse(result.data).data;
        console.log(img);
        this.bargin_ruleForm.pos_pic = img;
        console.log(result);
      } else {
      }
    },
    async query_xp_goods() {
      this.queryparam1.for_src=this.twitter?'4':'3'
      const data = await query_xp_goods(this.queryparam1);
      console.log(data);
      this.productList = data.data;
    },
    upload() {
      this.$refs.upimg.upload();
    },
    onInput() {},
    confirm_time1(e) {
      console.log(e);
      this.countdown = e.result;
      this.bargin_ruleForm.hour = e.result.split(":")[0];
      this.bargin_ruleForm.minutes = e.result.split(":")[1];
      console.log(this.bargin_ruleForm);
      this.show_time_picker = false;
    },
    cancle_time1() {
      this.show_time_picker = false;
    },
    change_need_times(e) {
      this.bargin_ruleForm.need_times = e;
    },
    change_per_times(e) {
      this.bargin_ruleForm.one_people_times = e;
    },
    change_cut_title(e) {
      this.bargin_ruleForm.cut_item_title = e;
    },
    change_cut_price(e) {
      this.bargin_ruleForm.cut_price = e;
    },
    change_suc_times(e) {
      this.bargin_ruleForm.success_times = e;
    },
    change_open_times(e) {
      this.bargin_ruleForm.open_times = e;
    },
    change_limit_radius(e) {
      this.bargin_ruleForm.limit_radius_pub = e;
    },
    choose_time() {
      this.show_time_picker = true;
    },
    change_show_need_times(e) {
      console.log(e);
      this.bargin_ruleForm.show_need_times = e.detail ? 1 : 0;
    },
    change_show_bj_price(e) {
      console.log(e);
      this.bargin_ruleForm.show_bj_price = e.detail ? 1 : 0;
    },
    async submit() {
      console.log(this.bargin_ruleForm);
      console.log();
      if (this.can_submit) {
        this.can_submit = false;
        if (this.twitter) {
          if (
            (this.bargin_ruleForm.need_times == "" &&
              this.bargin_ruleForm.need_times != 0) ||
            this.bargin_ruleForm.one_people_times == "" ||
            this.bargin_ruleForm.cut_item_title == "" ||
            this.bargin_ruleForm.pos_pic == "" ||
            this.bargin_ruleForm.price == "" ||
            (this.bargin_ruleForm.open_times == "" &&
              this.bargin_ruleForm.open_times != 0) ||
            (this.bargin_ruleForm.success_times == "" &&
              this.bargin_ruleForm.success_times != 0) ||
            (this.bargin_ruleForm.cut_price == "" &&
              this.bargin_ruleForm.cut_price != 0)
          ) {
            this.can_submit = true;
            this.$toast("请填写完整数据");
            return;
          }
        } else {
          if (
            this.countdown == "" ||
            (this.bargin_ruleForm.need_times == "" &&
              this.bargin_ruleForm.need_times != 0) ||
            this.bargin_ruleForm.one_people_times == "" ||
            this.bargin_ruleForm.cut_item_title == "" ||
            this.bargin_ruleForm.pos_pic == "" ||
            this.bargin_ruleForm.price == "" ||
            (this.bargin_ruleForm.open_times == "" &&
              this.bargin_ruleForm.open_times != 0) ||
            (this.bargin_ruleForm.success_times == "" &&
              this.bargin_ruleForm.success_times != 0) ||
            (this.bargin_ruleForm.cut_price == "" &&
              this.bargin_ruleForm.cut_price != 0)
          ) {
            this.can_submit = true;
            this.$toast("请填写完整数据");
            return;
          }
        }

        this.bargin_ruleForm.cut_price = Number(this.bargin_ruleForm.cut_price);
        this.bargin_ruleForm.hour = Number(this.bargin_ruleForm.hour);
        this.bargin_ruleForm.minutes = Number(this.bargin_ruleForm.minutes);
        this.bargin_ruleForm.need_times = Number(
          this.bargin_ruleForm.need_times
        );
        this.bargin_ruleForm.one_people_times = Number(
          this.bargin_ruleForm.one_people_times
        );
        this.bargin_ruleForm.open_times = Number(
          this.bargin_ruleForm.open_times
        );
        this.bargin_ruleForm.price = Number(this.bargin_ruleForm.price);
        this.bargin_ruleForm.success_times = Number(
          this.bargin_ruleForm.success_times
        );
        this.bargin_ruleForm.show_need_times = Number(
          this.bargin_ruleForm.show_need_times
        );
        this.bargin_ruleForm.show_bj_price = Number(
          this.bargin_ruleForm.show_bj_price
        );
        this.bargin_ruleForm.limit_radius_pub = Number(
          this.bargin_ruleForm.limit_radius_pub
        );
        if (this.from == "near") {
          this.bargin_ruleForm.cut_price = Number(this.bargin_ruleForm.price);
        }
        if (this.from == "twitter") {
          this.bargin_ruleForm.limit_radius_pub = 0;
        } else {
          if (this.bargin_ruleForm.limit_radius_pub == "") {
            this.bargin_ruleForm.limit_radius_pub = 0;
          } else {
            this.bargin_ruleForm.limit_radius_pub = Number(
              this.bargin_ruleForm.limit_radius_pub
            );
          }
        }
        try {
          const data = await item_save(this.bargin_ruleForm);
          if (data.code == 0) {
            this.$toast.success("添加成功");
            let currentPages =  getCurrentPages();
           let pages = currentPages[currentPages.length-2]
           Object.assign(this.bargin_ruleForm,{approve_status:'0'})
           //#ifdef APP-PLUS
               pages.$vm.goodList.push(this.bargin_ruleForm)
              setTimeout(() => {
                uni.navigateBack({
                   delta: 1
                });
              }, 1000);
            //#endif
           //#ifdef H5
               pages.goodList.push(this.bargin_ruleForm)
              setTimeout(() => {
                uni.navigateBack({
                   delta: 1
                });
              }, 1000);
            //#endif
           
            // this.rule_show = false;
            // setTimeout(() => {
            //   this.can_submit = true;
            // }, 1000);
          }
        } catch (err) {
          console.log(err);
          setTimeout(() => {
            this.can_submit = true;
          }, 1000);
          this.$toast.success("添加失败");
            setTimeout(() => {
              this.can_submit = true;
            }, 1000);
        }
        // this.bargin_ruleForm = {
        //   item_id: "",
        //   p: "XP",
        //   status: 1,
        //   hour: "2",
        //   minutes: "0",
        //   need_times: "",
        //   one_people_times: 1,
        //   cut_item_title: "",
        //   pos_pic: "",
        //   price: 0,
        //   open_times: 0,
        //   success_times: 0,
        //   cut_price: "",
        //   cut_mode: "2",
        //   show_need_times: 0,
        //   show_bj_price: 0,
        //   limit_radius_pub: 0,
        // };
      }
    },
    cancle_submit() {
      console.log("111");

      (this.bargin_ruleForm = {
        item_id: "",
        p: "XP",
        status: 0,
        hour: "",
        minutes: "",
        need_times: "",
        one_people_times: 1,
        cut_item_title: "",
        pos_pic: "",
        price: 0,
        open_times: 0,
        success_times: 0,
        cut_price: "",
        cut_mode: "",
        show_need_times: 0,
        show_bj_price: 0,
        limit_radius_pub: "",
      }),
        (this.uImgList = []);
      this.rule_show = false;
    },
    choose_bargin(e) {
      console.log(e);
      this.bargin_ruleForm.item_id = e.goods_code;
      this.bargin_ruleForm.pos_pic = e.cover_image;
      this.bargin_ruleForm.price = e.price2;
      this.bargin_ruleForm.cut_item_title = e.goods_title;
      console.log(this.bargin_ruleForm);
      this.rule_show = true;
    },
  },
 async onReachBottom() {
    console.log('触底了');
    if(this.rule_show) return
             this.queryparam1.page_index++;
      try {
        const data = await query_xp_goods(this.queryparam1);
        console.log(data);
        if (data.code == 0) {
          if (data.data.length == 0) {
            this.queryparam1.page_index--;
            uni.showToast({
              title: '全部加载',
              duration: 2000,
              icon:'none'
            });
          } else {
            this.productList.push(...data.data);
          }
        }
      } catch (error) {}
    // uni.$emit('onReachBottom');
  },
  onHide() {},
};
</script>

<style scoped lang="scss" scoped>
/deep/ .van-cell__title {
  width: 250upx;
  max-width: 250upx !important;
}
.count_down {
  display: flex;
}
.start_pick_container {
  display: flex;
  align-items: center;
  position: relative;
  width: 100%;
  .placeholder {
    position: absolute;
    top: 50%;
    left: 130px;
    transform: translateY(-50%);
    color: #333333;
  }
}
.upload {
  display: flex;
  align-items: center;
}
.btn_box {
  padding: 0 20px;
  margin-top: 20upx;
  /deep/ .van-button:first-child {
    margin-bottom: 20upx;
  }
}
.create_acativty {
  width: 100%;
  height: 96px;
  background: linear-gradient(90deg, #fdb482 0%, #f95a4b 100%);
  line-height: 96px;
  text-align: center;
  font-size: 36px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  margin-top: 40px;
}
/deep/ .txt-r {
  text-align: left !important;
  padding-left: 20upx !important;
  margin-right: 0 !important;
}
/deep/ uni-textarea.text {
  width: 92% !important;
}
</style>
